home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ASME's Mechanical Engine…ing Toolkit 1997 December
/
ASME's Mechanical Engineering Toolkit 1997 December.iso
/
c_lang
/
duncan01.lzh
/
SHELL.C
next >
Wrap
Text File
|
1987-08-11
|
2KB
|
115 lines
#include <stdio.h>
#include <process.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#define dim(x) (sizeof(x)/sizeof(x[0]))
typedef enum {false = 0,true = !false} boolean;
/* You may have to change these function prototypes a little. */
/* Or maybe not... */
int intrinsic(char *);
void extrinsic(char *);
void get_cmd(char *);
void get_comspec(char *);
int break_handler(void);
int cls_cmd(void);
int dos_cmd(void);
int exit_cmd(void);
struct cmd_table
{
char *cmd_name;
int (*cmd_fxn)(void);
} commands[] = {"CLS",cls_cmd,"DOS",dos_cmd,"EXIT",exit_cmd};
static char com_spec[64];
main()
{
char inp_buf[80];
get_comspec(com_spec);
if (ssignal(SIGINT,break_handler) == SIG_ERR)
{
fputs("Can't capture ctrl-c interrupt",stderr);
exit(1);
}
while (1)
{
get_cmd(inp_buf);
if (!intrinsic(inp_buf))
extrinsic(inp_buf);
}
}
int intrinsic(input_string)
char *input_string;
{
int i,j;
while (*input_string == 0x20)
input_string++;
for (i = 0;i < dim(commands);i++)
{
j = strcmp(commands[i].cmd_name,input_string);
if (j == 0)
{
(*commands[i].cmd_fxn)();
return true;
}
}
return false;
}
void extrinsic(input_string)
char *input_string;
{
if (system(input_string) != 0)
fputs("\nEXEC of COMMAND.COM failed\n",stderr);
}
void get_cmd(buffer)
char *buffer;
{
printf("\nsh: ");
gets(buffer);
strupr(buffer);
}
void get_comspec(buffer)
char *buffer;
{
strcpy(buffer,getenv("COMSPEC"));
if (buffer[0] == NULL)
{
fputs("\nNo COMSPEC variable in Environment\n",stderr);
exit(1);
}
}
int break_handler(void)
{
ssignal(SIGINT,break_handler);
printf("\nsh: ");
}
int cls_cmd(void)
{
printf("\033[2J");
}
int dos_cmd(void)
{
if (spawnlp(P_WAIT,com_spec,com_spec,NULL) != 0)
fputs("\nEXEC of COMMAND.COM failed\n",stderr);
}
int exit_cmd(void)
{
exit(0);
}